package com.zx.easy.controller;


import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.zx.easy.myutil.StyleUtils;
import com.zx.easy.pojo.vo.UserExportVo;
import com.zx.easy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
@Slf4j
public class EasyExcelController {
    
    @Resource
    UserService userService;

    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        List<UserExportVo> voList = userService.testMapStruct();

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 设置样式
        WriteCellStyle headStyle = StyleUtils.getHeadStyle();
        WriteCellStyle contentStyle = StyleUtils.getContentStyle();
        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headStyle, contentStyle);

        EasyExcel.write(response.getOutputStream(), UserExportVo.class).sheet("模板").registerWriteHandler(styleStrategy).doWrite(voList);
    }

    @PostMapping("upload")
    public String upload(@RequestBody MultipartFile file) throws IOException {
        return userService.upload(file);
    }

    @GetMapping("complex")
    public void complex() throws IOException {
        List<UserExportVo> voList = userService.testMapStruct();

        long l = System.currentTimeMillis();
        String templateFileName = EasyExcelController.class.getResource("/").getPath()+"excel"+"/weekmonth.xlsx";

        String mainHead ="2019年10月9日13:28:28";
        
        String fileName =  "D:/周月导出"+System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build();
        for (int i = 0; i < 100; i++) {
            excelWriter.fill(voList, fillConfig, writeSheet);      
        }
      
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mainHead",mainHead );
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();
        log.error("用时：{}", DateUtil.spendMs(l));
    }

}
